package com.example.mywechat;


import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Adapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

//接口的目的是添加全局监听
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    //实例化fragement对象
    private Fragment mTab01=new weixinFragment();
    private Fragment mTab02=new frdFragment();
    private Fragment mTab03=new contactFragment();
    private Fragment mTab04=new settingFragment();
    private FragmentManager fm;


    private Context context;
//    private RecyclerView friendRecyclerView;

//    private List<friendbean> list = new ArrayList<>();

    private Adapter adapter;


    private LinearLayout mTabWeixin;
    private LinearLayout mTabFrd;
    private LinearLayout mTabContacts;
    private LinearLayout mTabSettings;

    private ImageButton mImgWeixin;
    private ImageButton mImgFrd;
    private ImageButton mImgContacts;
    private ImageButton mImgSettings;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        取消开始标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        RecyclerView recyclerView = findViewById(R.id.friendrecycler_view);
        //布局管理器

        initView();
        initFragment();
        selectfragment(0);
        initEvent();
    }
    //把fragment组件转化为java对象，然后提交
    private void initFragment(){
        //fm是管理器
         fm = getSupportFragmentManager();
         //transaction是事务的意思，要么全成功，要么全不成功
        FragmentTransaction transaction=fm.beginTransaction();
        transaction.add(R.id.id_content,mTab01);
        transaction.add(R.id.id_content,mTab02);
        transaction.add(R.id.id_content,mTab03);
        transaction.add(R.id.id_content,mTab04);
        transaction.commit();
    }
    //把页面的组件通过id获取，封装为一个java对象
    private void initView(){
        mTabWeixin=(LinearLayout)findViewById(R.id.id_weichat);
        mTabFrd=(LinearLayout)findViewById(R.id.id_friend);
        mTabContacts=(LinearLayout)findViewById(R.id.id_contact);
        mTabSettings=(LinearLayout)findViewById(R.id.id_setting);

        mImgWeixin=findViewById(R.id.imageButton1);
        mImgFrd=findViewById(R.id.imageButton2);
        mImgContacts=findViewById(R.id.imageButton3);
        mImgSettings=findViewById(R.id.imageButton4);

    }


    private void hidefragment(FragmentTransaction transaction){
        transaction.hide(mTab01);
        transaction.hide(mTab02);
        transaction.hide(mTab03);
        transaction.hide(mTab04);


    }

//选着然后提交事务
    private void selectfragment(int i){
        //开启fm管理器为一个事务
        FragmentTransaction transaction=fm.beginTransaction();
        hidefragment(transaction);
        switch (i){
            case 0:
                transaction.show(mTab01);
                mImgWeixin.setImageResource(R.drawable.tab_weixin_pressed);
                break;

            case 1:
                transaction.show(mTab02);
                mImgFrd.setImageResource(R.drawable.tab_find_frd_pressed);
                break;
            case 2:
                transaction.show(mTab03);
                mImgContacts.setImageResource(R.drawable.tab_address_pressed);
                break;
            case 3:
                transaction.show(mTab04);
                mImgSettings.setImageResource(R.drawable.tab_settings_pressed);
                break;
            default:
                break;
        }
        transaction.commit();
    }

    @Override
// v.getId()获取到当前视图的id,点击事件
    public void onClick(View v) {
        resetimg();
       switch (v.getId()){
           case R.id.id_weichat:
               selectfragment(0);
               break;
           case R.id.id_friend:
               selectfragment(1);
               break;
           case R.id.id_contact:
               selectfragment(2);
                break;
           case R.id.id_setting:
               selectfragment(3);
               break;
           default:
               break;
       }

    }

    public void resetimg(){
        mImgWeixin.setImageResource(R.drawable.tab_weixin_normal);
        mImgFrd.setImageResource(R.drawable.tab_find_frd_normal);
        mImgContacts.setImageResource(R.drawable.tab_address_normal);
        mImgSettings.setImageResource(R.drawable.tab_settings_normal);
    }
//设置在activity中的监听范围为这四个
    private void initEvent(){

        mTabContacts.setOnClickListener(this);
      mTabFrd.setOnClickListener(this);
        mTabSettings.setOnClickListener(this);
        mTabWeixin.setOnClickListener(this);
    }
}